﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FlightSys.FsFsdb;

namespace FsDump
{
    public partial class ConfigFrm : Form
    {
        public ConfigFrm()
        {
            InitializeComponent();
            this.textBox1.Text = MainClass.GpsDumpPath;
            this.textBox2.Text = MainClass.FsdbPath;
            this.textBox3.Text = MainClass.TnpPath;
            this.textBox4.Text = MainClass.FlightsPath;
            //this.CHK_CUSTOM.Checked = MainClass.UseCustomID;
            this.CHK_POFF.Checked = MainClass.Poff;
            this.CHK_FILTER.Checked = MainClass.Usefilter;
            this.NB_INT.Value = MainClass.Interfaces;
            this.NB_UTCDIFF.Value = MainClass.Utcdiff;
            //this.TXT_CUSTOM.Text = MainClass.CustomID;
            this.CBO_MODE.SelectedIndex = (int)MainClass.Mode;
            try
            {
                this.DT_FILTER.Value = MainClass.FilterTime;
            }
            catch {
                this.DT_FILTER.Value = DateTime.Now;
            }
        }

        private void BT_GPSDUMP_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "GPSDUMP ABOVE VERSION 4.31" ;
			fdlg.Filter = "GPSDUMP (GpsDump*.*)|GpsDump*.*" ;
			fdlg.FilterIndex = 1 ;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                MainClass.GpsDumpPath = fdlg.FileName;
                MainClass.ifr.SetIniValue("Settings", "GpsDumpPath", MainClass.GpsDumpPath);
            }
            this.textBox1.Text = MainClass.GpsDumpPath;
        }

        private void BT_FSDB_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "FSCOMPE FILE ABOVE VERSION 1.26";
            fdlg.Filter = "FsCompe (*.fsdb)|*.fsdb";
            fdlg.FilterIndex = 1;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                MainClass.FsdbPath = fdlg.FileName;
                MainClass.ifr.SetIniValue("Settings", "FsdbPath", MainClass.FsdbPath);
                //TODO: read the fscompe file and search for Tasks Filter and flights path
            }
            textBox2.Text = MainClass.FsdbPath;
        }

        private void BT_TURNPOINTS_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "GPSDUMP OZI TURNPOINTS";
            fdlg.Filter = "GPSDUMP OZI TURNPOINTS (*.wpt)|*.wpt";
            fdlg.FilterIndex = 1;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                MainClass.TnpPath = fdlg.FileName;
                MainClass.ifr.SetIniValue("Settings", "TnpPath", MainClass.TnpPath);
            }
            textBox3.Text = MainClass.TnpPath;

        }

        private void BT_PATH_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            //fbd.RootFolder=System.Environment.SpecialFolder.MyComputer;
            fbd.ShowNewFolderButton = true;
            fbd.Description = "Please select a folder to store downloaded flights.";
            if (!(MainClass.GpsDumpPath == string.Empty))
            {
                fbd.SelectedPath = MainClass.GpsDumpPath.Remove(MainClass.GpsDumpPath.LastIndexOf("\\"), MainClass.GpsDumpPath.Length - MainClass.GpsDumpPath.LastIndexOf("\\"));
            }
            if (!((MainClass.FsdbPath) == string.Empty))
            {
                fbd.SelectedPath = MainClass.FsdbPath.Remove(MainClass.FsdbPath.LastIndexOf("\\"), MainClass.FsdbPath.Length - MainClass.FsdbPath.LastIndexOf("\\"));
            }

            DialogResult result = fbd.ShowDialog();
            if (result == DialogResult.OK)
            {
                MainClass.FlightsPath = fbd.SelectedPath;
                MainClass.ifr.SetIniValue("Settings", "FlightsPath", MainClass.FlightsPath);
            }
            textBox4.Text = MainClass.FlightsPath;

        }

        private void CHK_FILTER_CheckedChanged(object sender, EventArgs e)
        {
            MainClass.Usefilter = this.CHK_FILTER.Checked;
            MainClass.ifr.SetIniValue("Settings", "Usefilter", MainClass.Usefilter.ToString());
        }

        private void CHK_POFF_CheckedChanged(object sender, EventArgs e)
        {
            MainClass.Poff = this.CHK_POFF.Checked;
            MainClass.ifr.SetIniValue("Settings", "Poff", MainClass.Poff.ToString());
        }

        private void CHK_CUSTOM_CheckedChanged(object sender, EventArgs e)
        {
            //MainClass.UseCustomID = this.CHK_CUSTOM.Checked;
            MainClass.ifr.SetIniValue("Settings", "UseCustomID", MainClass.UseCustomID.ToString());
        }

        private void CBO_MODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            MainClass.Mode = this.CBO_MODE.SelectedIndex;
            MainClass.ifr.SetIniValue("Settings", "Mode", MainClass.Mode.ToString());
        }

        private void DT_FILTER_ValueChanged(object sender, EventArgs e)
        {
            MainClass.FilterTime = this.DT_FILTER.Value;
            MainClass.ifr.SetIniValue("Settings", "FilterTime", MainClass.FilterTime.ToString("yyyy/MM/dd HH:mm:ss"));
        }

        private void NB_INT_ValueChanged(object sender, EventArgs e)
        {
            MainClass.Interfaces = this.NB_INT.Value;
            MainClass.ifr.SetIniValue("Settings", "Interfaces", MainClass.Interfaces.ToString());
        }

        private void NB_UTCDIFF_ValueChanged(object sender, EventArgs e)
        {
            MainClass.Utcdiff = this.NB_UTCDIFF.Value;
            MainClass.ifr.SetIniValue("Settings", "Utcdiff", MainClass.Utcdiff.ToString());
        }

        private void BT_OK_Click(object sender, EventArgs e)
        {
            MainClass.Fs = Fsdb.Open(MainClass.FsdbPath);
            MainClass.SaveConfig();
        }

    }
}
